package com.zhangzhan.service.impl;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.service.UploadService;
import com.zhangzhan.utils.PathUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author 张一恒
 * @version 1.0
 * @description: 上传文件
 * @date 2022/12/3 21:02
 */
@Service
@Slf4j
@ConfigurationProperties("oos")
@Data
public class OssUploadService implements UploadService {

    private String accessKey;
    private String secretKey;
    private String bucket;

    /**
     * @param img
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @description 上传头像
     * @author 31115
     * @date 2022/12/3 21:03
     */
    @Override
    public ResponseResult upload(MultipartFile img) {

        //判断是否为png后缀名的文件
        if (!img.getOriginalFilename().endsWith(".png")){
            return ResponseResult.errorResult(403,"后缀名不是png的文件");
        }

        String pathName = PathUtils.generateFilePath(img.getOriginalFilename());
        String imgUrl = OOSUploadUrl(img,pathName);
       return ResponseResult.okResult(imgUrl);
    }

    private String OOSUploadUrl(MultipartFile imgFile,String pathName) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        //cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        //...其他参数参考类注释

        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传

        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = pathName;

        try {

            //上传的头像文件
            InputStream inputStream = imgFile.getInputStream();

            //ak sk 文件名
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);

            try {
                Response response = uploadManager.put(inputStream, key, upToken, null, null);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
                return "http://rmb5jnyv9.hn-bkt.clouddn.com/" + putRet.key;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
